package com.itlong.cloud.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.app.old.AppOldUserInfoDto;
import com.itlong.cloud.POJO.PO.AppUserInfoPO;
import com.itlong.cloud.POJO.PO.AppUserProjectPO;
import com.itlong.cloud.POJO.PO.ProjectPO;
import com.itlong.cloud.POJO.VO.app.old.AppOldUserInfoVO;
import com.itlong.cloud.POJO.VO.app.old.AppOldUserOwnProjectVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.app.dao.IAppOldRegisterDao;
import com.itlong.cloud.app.dao.IAppRegisterDao;
import com.itlong.cloud.app.service.IAppOldRegisterService;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.enumerate.FileCatalogEnum;
import com.itlong.cloud.enumerate.oldapp.Code;
import com.itlong.cloud.property.dao.IPropertyAppUserProjectDao;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.encrypt.UserTokenUtil;
import com.itlong.cloud.utils.file.UploadFileUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *     旧版APP_登录注册板块服务层实现类
 * </desc>
 *
 * @createDate 2017/10/17
 */
@Service
@RefreshScope
public class AppOldRegisterServiceImpl  implements IAppOldRegisterService {
    private static final Logger LOG = LoggerFactory.getLogger(AppOldRegisterServiceImpl.class);

    @Value("${" + PlatformConstants.APP_USER_TOKEN_KEY + "}")
    public String APP_USER_TOKEN_KEY;

    @Value("${" + PlatformConstants.FILE_FTP_HOST + "}")
    public String FILE_FTP_HOST;

    @Value("${" + PlatformConstants.FILE_FTP_PORT + "}")
    public String FILE_FTP_PORT;

    @Value("${" + PlatformConstants.FILE_FTP_USERNAME + "}")
    public String FILE_FTP_USERNAME;

    @Value("${" + PlatformConstants.FILE_FTP_PASSWORD + "}")
    public String FILE_FTP_PASSWORD;

    @Value("${" + PlatformConstants.FILE_ACCESS_URL + "}")
    public String FILE_ACCESS_URL;



    @Autowired
    IAppOldRegisterDao iAppOldRegisterDao;

    @Autowired
    IAppRegisterDao iAppRegisterDao;

    @Autowired
    RedisService redisService;

    @Autowired
    IPropertyAppUserProjectDao iPropertyAppUserProjectDao;

    /**
     * <desc>
     *     根据用户输入对象查询用户响应对象集合
     * </desc>
     * @param appOldUserInfoDto
     * @return
     * @author cuiduanwu
     * @createDate 2017/10/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public List selectVOByDTO(AppOldUserInfoDto appOldUserInfoDto){
        return  iAppOldRegisterDao.selectVOByDTO(appOldUserInfoDto);
    }


    /**
     * <desc>
     *     根据手机号查询用户VO
     * </desc>
     * @param phoneNo
     * @return
     * @author cuiduanwu
     * @createDate 2017/10/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public AppOldUserInfoVO selectVOByPhone(String phoneNo){
        return  iAppOldRegisterDao.selectVOByPhone(phoneNo);
    }

    /**
     * <desc>
     *     根据手机号查询用户DTO
     * </desc>
     * @param phoneNo
     * @return
     * @author cuiduanwu
     * @createDate 2017/10/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public AppOldUserInfoDto selectDtoByPhone(String phoneNo){
        return  iAppOldRegisterDao.selectDtoByPhone(phoneNo);
    }

    /**
     * <desc>
     *     根据用户DTO更新用户信息
     * </desc>
     * @param appOldUserInfoDto
     * @return
     * @author cuiduanwu
     * @createDate 2017/10/18
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean updateNotNullByAppOldUserInfoDto(AppOldUserInfoDto appOldUserInfoDto){
        return iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(appOldUserInfoDto)>0?true:false;
    }

    /**
     * <desc>
     *     保存旧版App用户对象至缓存
     * </desc>
     * @param appOldUserInfoDto
     * @return
     * @author cuiduanwu
     * @createDate 2017/10/18
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public AppOldUserInfoDto saveCacheUser(AppOldUserInfoDto appOldUserInfoDto){
        appOldUserInfoDto.setValiDate(String.valueOf(System.currentTimeMillis()+RedisConstant.OLD_APP_LOGIN_TIME*1000)); //7天有效
        boolean flag = redisService.set(RedisConstant.OLD_APP_LOGIN_KEY + appOldUserInfoDto.getUserPhone(), RedisConstant.OLD_APP_LOGIN_TIME,appOldUserInfoDto);
        if (StringUtils.isEmpty(appOldUserInfoDto.getUserId())) {
            flag = redisService.set(RedisConstant.OLD_APP_LOGIN_KEY + appOldUserInfoDto.getUserPhone(),RedisConstant.OLD_APP_REGISTER_TIME, appOldUserInfoDto);
            flag = redisService.set(RedisConstant.OLD_APP_REGISTER_KEY + appOldUserInfoDto.getUserPhone(),RedisConstant.OLD_APP_REGISTER_TIME, appOldUserInfoDto); // 缓存注册时的密码,5分钟
        }
        if (flag){
            return  appOldUserInfoDto;
        }else {
            return  null;
        }
    }
    /**
     * <desc>
     *     旧版APP_注册提交
     * </desc>
     * @param appOldUserInfoVO
     * @param pswd   已加密密码
     * @return
     * @author cuiduanwu
     * @createDate 2017/10/18
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Code registerProjectInfo(AppOldUserInfoVO appOldUserInfoVO,String pswd) throws  Exception{
        Code code = Code.ERROR;
        try {
            String phoneNo = appOldUserInfoVO.getPhoneNo();
            AppOldUserInfoDto cacheUser = (AppOldUserInfoDto)redisService.getObject(RedisConstant.OLD_APP_LOGIN_KEY + phoneNo,AppOldUserInfoDto.class);
            if (cacheUser == null){
                cacheUser = iAppOldRegisterDao.selectDtoByPhone(phoneNo);
            }
            if (cacheUser == null){
                code = Code.NOT_FOUNDUSER;
            }else {
                if (cacheUser.getToken().equals(appOldUserInfoVO.getToken())){
                    cacheUser.setUserName(appOldUserInfoVO.getUserName());
                    cacheUser.setNickName(appOldUserInfoVO.getNickName());
                    if (StringUtils.isBlank(cacheUser.getNickName())){
                        cacheUser.setNickName(phoneNo.substring(0, 3) + "****" + phoneNo.substring(phoneNo.length() - 4, phoneNo.length()));
                    }
                    cacheUser.setUserPhone(phoneNo);
                    cacheUser.setSex(appOldUserInfoVO.getSex());
                    cacheUser.setBirthday(appOldUserInfoVO.getBirthday());
                    cacheUser.setUserMemo(appOldUserInfoVO.getUserMemo());
                    cacheUser.setPswd(pswd);
                    cacheUser.setCreateTime(new Date());

                    AppUserInfoPO appUserInfoPO = new AppUserInfoPO();
                    if (StringUtils.isBlank(cacheUser.getUserId())){ //注册用户
                        String userId = UserTokenUtil.tokenDecrypt(cacheUser.getToken(),APP_USER_TOKEN_KEY);
                        cacheUser.setUserId(userId);
                        cacheUser.setUserType(1);
                        cacheUser.setUserStatus(0);
                        //保存APP用户信息
                        int saveUserCount = iAppOldRegisterDao.saveAppOldUser(cacheUser);
                        //TODO   构建用户与项目关系
                        AppUserProjectPO appUserProjectPO = new AppUserProjectPO(cacheUser.getUserId(),appOldUserInfoVO.getCurrentProjectId(),"0","000000","1",1,1,new Date());
                        int saveUserProjectCount = iPropertyAppUserProjectDao.save(SqlUtil.durableData(appUserProjectPO, PlatformConstants.TABLE_SAVE));
                        if (saveUserCount>0&&saveUserProjectCount>0){
                            this.saveCacheUser(cacheUser);
                            code = Code.SUCCESS;
                        }else {
                            code = Code.ERROR;
                        }
                    }else { //预授权用户
                        int updateCount = iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(cacheUser);
                        if (updateCount>0){
                            this.saveCacheUser(cacheUser);
                            code = Code.SUCCESS;
                        }else {
                            code = Code.ERROR;
                        }
                    }



                }else {
                    code = Code.TOKEN_ERR;
                }
            }
        }catch (Exception e){
            throw new DataAccessException("【旧版APP_登录注册版块】注册提交失败:保存用户或用户项目关系失败",e);
        }
        return  code;
    }
    /**
     * <desc>
     *     旧版APP_登录
     * </desc>
     * @param phoneNo
     * @param pswd 已加密的密码
     * @param loginDevice
     * @return
     * @throws Exception
     * @author cuiduanwu
     * @createDate 2017/10/23
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public AppOldUserInfoVO appLogin(String phoneNo,String pswd,String loginDevice) throws  Exception{
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterDao.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto != null&&pswd.equals(appOldUserInfoDto.getPswd())){
            AppOldUserInfoDto userDto = new AppOldUserInfoDto();
            userDto.setUserId(appOldUserInfoDto.getUserId());
            userDto.setLoginDevice(Integer.parseInt(loginDevice));
            iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(userDto);

            AppOldUserInfoVO appOldUserInfoVO = new AppOldUserInfoVO();
            BeanUtils.copyProperties(appOldUserInfoDto,appOldUserInfoVO);

            appOldUserInfoVO.setUserStatus(appOldUserInfoDto.getUserStatus());

            JSONObject js = this.getUserProjectInfo(appOldUserInfoDto.getUserId(),"");
            appOldUserInfoVO.setProjectIds(js.getString("projectIds"));
            appOldUserInfoVO.setProjectNames(js.getString("projectNames"));
            appOldUserInfoVO.setUserTypeInProject(js.getString("userTypeInProject"));
            appOldUserInfoVO.setUserAuth(js.getString("userAuth"));
            appOldUserInfoVO.setAuthMaxNum(js.getString("authMaxNum"));
            appOldUserInfoVO.setAuthNum(js.getString("authNum"));

            appOldUserInfoVO.setAroundProjectIds(js.getString("aroundProjectIds"));
            appOldUserInfoVO.setAroundProjectNames(js.getString("aroundProjectNames"));
            appOldUserInfoVO.setCurrentProjectId(js.getString("currentProjectId"));
            appOldUserInfoVO.setCurrentProjectName(js.getString("currentProjectName"));
            appOldUserInfoVO.setServerUrls(js.getString("serverUrls"));
            appOldUserInfoVO.setPhoneNo(phoneNo);
            appOldUserInfoDto.setCurrentProjectId(js.getString("currentProjectId"));
            appOldUserInfoDto.setCurrentProjectName(js.getString("currentProjectName"));

            //缓存用户
            this.saveCacheUser(appOldUserInfoDto);
            this.saveCacheUserVO(appOldUserInfoVO);
            return  appOldUserInfoVO;
        }else {
            LOG.info("【旧版APP_登录注册版块】登录失败:手机号或密码错误");
            return  null;
        }
    }

    /**
     * <desc>
     *     旧版APP_缓存用户响应数据VO
     * </desc>
     * @param appOldUserInfoVO
     */
    @Override
    public AppOldUserInfoVO saveCacheUserVO(AppOldUserInfoVO appOldUserInfoVO){
        appOldUserInfoVO.setValiDate(String.valueOf(System.currentTimeMillis()+RedisConstant.OLD_APP_LOGIN_TIME*1000)); //7天有效
        boolean flag = redisService.set(RedisConstant.OLD_APP_USER_VO_KEY + appOldUserInfoVO.getPhoneNo(),RedisConstant.OLD_APP_LOGIN_TIME,appOldUserInfoVO);
        return  appOldUserInfoVO;
    }


    /**
     * <desc>
     *     旧版APP_获取用户项目附加信息
     * </desc>
     * @param userId
     * @param projectId
     * @return
     */
    @Override
    public JSONObject getUserProjectInfo(String userId,String projectId){
        JSONObject js = new JSONObject();
        String aroundProjectIds = "",aroundProjectNames="",projectIds = "",projectNames = "",pubKeys= "",userTypeInProject="",userAuth = "",authMaxNum = "",authNum = "",
                currentProjectId = "",currentProjectName = "",serverUrls = "",doorPwd = "",isAllowPhone = "",turnServer="",centerPhoneNo = "",ownerAddress="";
        AppUserProjectPO appUserProjectPO = new AppUserProjectPO(userId,projectId);

        List<AppOldUserOwnProjectVO> userOwnProjectVOList = iAppOldRegisterDao.getUserOwnProjectsInfo(appUserProjectPO);
        if (userOwnProjectVOList != null&&userOwnProjectVOList.size()>0){
            currentProjectId = userOwnProjectVOList.get(0).getProjectId();
            currentProjectName = userOwnProjectVOList.get(0).getProjectName();

            Map<String,Object> params = new HashMap<>();
            params.put("userId",userId);
            for (AppOldUserOwnProjectVO appOldUserOwnProjectVO:userOwnProjectVOList){
                if (StringUtils.isBlank(projectIds)){
                    projectIds = appOldUserOwnProjectVO.getProjectId();
                }else {
                    projectIds = projectIds+","+appOldUserOwnProjectVO.getProjectId();
                }
                if (StringUtils.isBlank(projectNames)){
                    projectNames = appOldUserOwnProjectVO.getProjectName();
                }else {
                    projectNames = projectNames+","+appOldUserOwnProjectVO.getProjectName();
                }
                if (StringUtils.isBlank(pubKeys)){
                    pubKeys = appOldUserOwnProjectVO.getPubKey();
                }else {
                    pubKeys = pubKeys+","+appOldUserOwnProjectVO.getPubKey();
                }
                if (StringUtils.isBlank(userTypeInProject)){
                    userTypeInProject = appOldUserOwnProjectVO.getUserType();
                    if (StringUtils.isBlank(userTypeInProject)){
                        userTypeInProject = "1";
                    }
                }else {
                    userTypeInProject = userTypeInProject+","+appOldUserOwnProjectVO.getUserType();
                }

                params.put("projectId",appOldUserOwnProjectVO.getProjectId());
                params.put("houserType","1");  //业主身份
                Integer userAuthCount = iAppOldRegisterDao.countUserAttacheInfoInProject(params);
                if (userAuthCount>0){
                    if (StringUtils.isBlank(userAuth)){
                        userAuth = "Y";
                        //userAuth = "N";
                        ownerAddress = "Y";
                    }else {
                        userAuth = userAuth+","+"Y";
                        //userAuth = userAuth+","+"N";
                        ownerAddress =ownerAddress + ",Y";
                    }
                }else {
                    if (StringUtils.isBlank(userAuth)){
                        userAuth = "N";
                    }else {
                        userAuth = userAuth+","+"N";
                    }
                }
                //获取用户已授权的二级用户数量
                Map<String,Object> userAuthMap = new HashMap<>();
                userAuthMap.put("projectId",appOldUserOwnProjectVO.getProjectId());
                userAuthMap.put("parentUserId",userId);
                Integer authCount = iAppOldRegisterDao.countUserAuthChild(userAuthMap);
                if (StringUtils.isBlank(authNum)){
                    authNum = authCount+"";
                }else {
                    authNum = authNum+","+authCount;
                }


                if (StringUtils.isBlank(authMaxNum)){
                    authMaxNum = appOldUserOwnProjectVO.getAuthMaxNum();
                    if (StringUtils.isBlank(authMaxNum)){
                        authMaxNum = "10";
                    }
                }else {
                    authMaxNum = authMaxNum+","+appOldUserOwnProjectVO.getAuthMaxNum();
                }
                if (StringUtils.isBlank(serverUrls)){
                    serverUrls = appOldUserOwnProjectVO.getServerUrl();
                }else {
                    serverUrls = serverUrls+","+appOldUserOwnProjectVO.getServerUrl();
                }
                if (StringUtils.isBlank(doorPwd)){
                    doorPwd = appOldUserOwnProjectVO.getOpenPwd();
                }else {
                    doorPwd = doorPwd+","+appOldUserOwnProjectVO.getOpenPwd();
                }
                if (StringUtils.isBlank(isAllowPhone)){
                    isAllowPhone = appOldUserOwnProjectVO.getIsAllowPhone();
                }else {
                    isAllowPhone = isAllowPhone+","+appOldUserOwnProjectVO.getIsAllowPhone();
                }
            }
        }
        if (StringUtils.isNotBlank(projectId)){
            ProjectPO projectPO = iAppOldRegisterDao.getProjectInfoByPk(projectId);
            if (projectPO != null){
                centerPhoneNo = projectPO.getManagePhone();
                turnServer = projectPO.getTurnServer();
            }
        }
        js.put("centerPhoneNo",centerPhoneNo);
        js.put("turnServer",turnServer);
        js.put("ownerAddress",ownerAddress);
        js.put("userId",userId);
        js.put("aroundProjectIds",aroundProjectIds);
        js.put("aroundProjectNames",aroundProjectNames);
        js.put("projectIds",projectIds);
        js.put("projectNames",projectNames);
        js.put("userTypeInProject",userTypeInProject);
        js.put("userAuth",userAuth);
        js.put("authMaxNum",authMaxNum);
        js.put("authNum",authNum);
        js.put("pubKeys",pubKeys);
        js.put("currentProjectId",currentProjectId);
        js.put("currentProjectName",currentProjectName);
        js.put("serverUrls",serverUrls);
        js.put("isAllowPhone",isAllowPhone.equals("0") ? "1":"0");
        js.put("doorPwd",doorPwd);
        return js;
    }


    /**
     *<desc>
     *     旧版APP_忘记密码提交
     *</desc>
     * @param phoneNo
     * @param pswd 已加密的密码
     * @return
     * @throws Exception
     * @author cuiduanwu
     * @createDate 2017/10/30
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Code appRetrievePswd(String phoneNo,String pswd) throws Exception{
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterDao.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto != null){
            AppOldUserInfoDto userDto = new AppOldUserInfoDto();
            userDto.setUserId(appOldUserInfoDto.getUserId());
            userDto.setPswd(pswd);
            iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(userDto);
            return  Code.SUCCESS;
        }else {
            return Code.NOT_FOUNDUSER;
        }
    }


    /**
     * <desc>
     *     旧版APP_更新头像
     * </desc>
     * @param userId
     * @param imageStr
     * @param imageType
     * @return
     * @createDate 2017/10/30
     * @author cuiduanwu
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public String updatePhoto(String userId,String imageStr,String imageType) throws Exception{
        String imageUrl = null;
        try {
            imageStr = URLDecoder.decode(imageStr, "UTF-8");
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bytes = decoder.decodeBuffer(imageStr);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            UploadFileUtil.UploadFile uploadFileUtil = new UploadFileUtil().new UploadFile(
                    FILE_FTP_HOST,
                    FILE_FTP_USERNAME,
                    FILE_FTP_PASSWORD,
                    FILE_FTP_PORT,
                    byteArrayInputStream,
                    imageType,
                    FILE_ACCESS_URL,
                    FileCatalogEnum.APPLOGO_FILE.getType()
            );
            Map<String, Object> resultMap = UploadFileUtil.uploadFile(uploadFileUtil);
            if ("true".equals(resultMap.get("returnCode").toString())){
                imageUrl = resultMap.get("accessPath").toString();
                AppOldUserInfoDto appOldUserInfoDto = new AppOldUserInfoDto();
                appOldUserInfoDto.setUserId(userId);
                appOldUserInfoDto.setPhotoUrl(imageUrl);
                int i = iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(appOldUserInfoDto);
                //LOG.info("【旧版APP_更新头像】全地址:"+imageUrl);
                if (i>0){
                    return  imageUrl;
                }else {
                    throw new DataAccessException("【旧版APP_更新头像】更新用户头像记录失败");
                }
            }else {
                throw new DataAccessException("【旧版APP_更新头像】更新用户头像上传失败");
            }
        }catch (Exception e){
            throw new DataAccessException("【旧版APP_更新头像】更新用户头像失败",e);
        }
    }
    /**
     * <desc>
     *     旧版APP_修改密码
     * </desc>
     * @param phoneNo
     * @param oldPswd  已加密的旧密码
     * @param newPswd  已加密的新密码
     * @return
     * @throws Exception
     * @createDate 2017/10/30
     * @author cuiduanwu
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Code appUpdatePswd(String phoneNo,String oldPswd,String newPswd) throws  Exception{
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterDao.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto != null){
            if (appOldUserInfoDto.getPswd().equals(oldPswd)){
                AppOldUserInfoDto userDto = new AppOldUserInfoDto();
                userDto.setUserId(appOldUserInfoDto.getUserId());
                userDto.setPswd(newPswd);
                iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(userDto);
                return  Code.SUCCESS;
            }else {
                return Code.LOGIN_ERROR;
            }
        }else {
            return Code.NOT_FOUNDUSER;
        }
    }
    /**
     * <desc>
     *     旧版APP_修改手机号
     * </desc>
     * @param phoneNo 旧手机号
     * @param loginPswd  已加密的密码
     * @param replacePhoneNo  新手机号
     * @return
     * @throws Exception
     * @createDate 2017/10/30
     * @author cuiduanwu
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Code appUpdatePhone(String phoneNo,String loginPswd,String replacePhoneNo) throws  Exception{
        AppOldUserInfoDto currentUser = iAppOldRegisterDao.selectDtoByPhone(replacePhoneNo);
        if (currentUser != null){
            return  Code.EXIST_PHONENO;
        }
        AppOldUserInfoDto oldUserInfoDto = iAppOldRegisterDao.selectDtoByPhone(phoneNo);
        if (oldUserInfoDto == null||(oldUserInfoDto != null&&!oldUserInfoDto.getPswd().equals(loginPswd))){
            return Code.PSWD_ERR;
        }

        AppOldUserInfoDto userDto = new AppOldUserInfoDto();
        userDto.setUserId(oldUserInfoDto.getUserId());
        userDto.setUserId(replacePhoneNo);
        iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(userDto);
        return  Code.SUCCESS;
    }






    /**
     * <desc>
     *     旧版APP_修改昵称
     * </desc>
     * @param phoneNo
     * @param nickName
     * @return
     * @throws Exception
     * @createDate 2017/10/30
     * @author cuiduanwu
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Code updateNickName(String phoneNo,String nickName) throws  Exception{
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterDao.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto != null){
            AppOldUserInfoDto userDto = new AppOldUserInfoDto();
            userDto.setUserId(appOldUserInfoDto.getUserId());
            userDto.setNickName(nickName);
            iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(userDto);
            return  Code.SUCCESS;
        }else {
            return Code.NOT_FOUNDUSER;
        }
    }

    /**
     * <desc>
     *     旧版APP_修改姓名
     * </desc>
     * @param phoneNo
     * @param userName
     * @return
     * @throws Exception
     * @createDate 2017/10/30
     * @author cuiduanwu
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Code appUpdateUserName(String phoneNo,String userName) throws  Exception{
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterDao.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto != null){
            AppOldUserInfoDto userDto = new AppOldUserInfoDto();
            userDto.setUserId(appOldUserInfoDto.getUserId());
            userDto.setUserName(userName);
            iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(userDto);
            return  Code.SUCCESS;
        }else {
            return Code.NOT_FOUNDUSER;
        }
    }

    /**
     * <desc>
     *     旧版APP_修改性别
     * </desc>
     * @param phoneNo
     * @param sex 性别：0男 1女
     * @return
     * @throws Exception
     * @createDate 2017/10/30
     * @author cuiduanwu
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Code updateUserSex(String phoneNo,String sex) throws  Exception{
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterDao.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto != null){
            AppOldUserInfoDto userDto = new AppOldUserInfoDto();
            userDto.setUserId(appOldUserInfoDto.getUserId());
            userDto.setSex(sex);
            iAppOldRegisterDao.updateNotNullByAppOldUserInfoDto(userDto);
            return  Code.SUCCESS;
        }else {
            return Code.NOT_FOUNDUSER;
        }
    }



    /**
     *  <desc>
     *      旧版APP_检查核验用户的token正确性
     *  </desc>
     * @param phoneNo
     * @param token
     * @return
     * @createDate 2017/10/30
     * @author cuiduanwu
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Code checkUserStatus(String phoneNo,String token){
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterDao.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto ==null||(appOldUserInfoDto != null&&!token.equals(appOldUserInfoDto.getToken()))){
            return Code.TOKEN_ERR;
        }else if (appOldUserInfoDto != null&&token.equals(appOldUserInfoDto.getToken())){
            return Code.SUCCESS;
        }else {
            return Code.SUCCESS;
        }
    }




}
